package in.muton.edushare.poll.components;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.common.EdusharePanel;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Poll;

public class DraftButton extends Button {
	private static final long serialVersionUID = 3402206405010446146L;
	private IModel<Poll> pollModel;
	private EdusharePanel panel;

	public DraftButton(String id, IModel<Poll> model, EdusharePanel panel) {
		super(id);
		pollModel = model;
		this.panel = panel;
	}

	@Override
	public boolean isVisible() {
		if(pollModel == null)
			return true;
		if (pollModel.getObject().getStatus() == Poll.POLL_STATUS_DRAFT
				&& pollModel.getObject().getCreator() == EduShareSession.get()
						.getLoggedInUser())
			return true;
		return false;
	}

	@Override
	public void onSubmit() {
		if(pollModel != null)
			panel.setError("This poll has been saved as draft. You may return to this poll and edit it anytime.");
	}

}
